// 整形有序数组的二分查找
// 二分查找的前提： 数组连续且有序
#include <stdio.h>
int search(int arr[], int k, int sz)
{
    int left = 0;
    int right = sz - 1;
    while (left <= right)
    {
        int mid = left + (right - left) / 2;
        if (arr[mid] < k)
        {
            left = mid + 1;
        }
        else if (arr[mid] > k)
        {
            right = mid - 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int k = 12;
    int sz = sizeof(arr) / sizeof(arr[0]);
    int res = search(arr, k, sz);
    if (res == -1)
    {
        printf("no exist");
    }
    else
    {
        printf("have found\n");
        printf("its index is %d", res);
    }
    return 0;
}